//
//  UserAccount.swift
//  Weibo
//
//  Created by 梁智 on 15/11/25.
//  Copyright © 2015年 lzfree. All rights reserved.
//

import UIKit

class UserAccount: NSObject, NSCoding {

    /// 用于调用access_token，接口获取授权后的access token
    var access_token: String?
    
    /// 一旦从服务器获得过期的时间，立刻计算准确的日期
    var expires_in: NSTimeInterval = 0 {
        didSet {
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    /// 过期日期
    var expires_date: NSDate?
    
    /// 当前授权用户的 UID
    var uid: String?
    
    /// 用户昵称
    var screen_name: String?
    
    /// 用户头像地址（大图），180×180像素
    var avatar_large: String?
    
    init(dict: [String: AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    override var description: String {
        let keys = ["access_token", "expires_in", "expires_date", "uid", "screen_name", "avatar_large"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
    
    
    // MARK: - "键值"归档和解档
    /// 归档 - 在把当前对象保存到磁盘前,将对象编码成二进制数据 - 跟网络的序列化很像
    ///
    /// - parameter aCoder: 编码器
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    
    
    /// 解档 - 从磁盘加载二进制文件,转换成对象时调用 - 跟网络的反序列化很像
    ///
    /// - parameter aDecoder: 解码器
    ///
    /// - returns: - 没有继承性,所有的对象只能解档出当前类对象
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
}
